#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <stdlib.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{   
    int fd;
    printf("-----正在打开----\n");
#ifdef RP
      fd = open("fifo1", O_RDONLY);
#else
      fd = open("fifo1", O_WRONLY);
#endif
    printf("-----打开成功----\n");

int data = 0;
ssize_t ret = 0;
#ifdef RP
      while(1){
        printf("---reading data---\n");
        ret = read(fd, &data, 4); // 阻塞的
        printf("read data is %d \n", data);

        // 当write进程退出后，解除阻塞，但未读到数据
        // if(ret == 0) break;  
        data = 0;
        usleep(200);
      }
#else
      for(int i=0; i<5;i++){
          data = i + 10;
          printf("----writing data---\n");
          sleep(1);
          write(fd, &data, 4);
          printf("--writed ok--\n");
      }
#endif

    close(fd); // 关闭管道
    printf("---over---\n");
    return 0;
}
